package com.extension.company.view.my.businessenter;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bigkoo.pickerview.utils.WheelPickerUtil;
import com.extension.company.admin.R;
import com.extension.company.common.permission.PermissionsActivity;
import com.extension.company.common.permission.PermissionsUtil;
import com.extension.company.common.camera.openCameraUtils;
import com.extension.company.common.mvp.BaseTitleActivity;
import com.extension.company.Constant;
import com.extension.company.common.utils.ToastUtil;
import com.extension.company.contract.my.SellerEnterContract;
import com.extension.company.presenter.my.SellerEnterPresenter;
import com.extension.company.common.widget.popuwindow.PwAddressListAddPopWindow;
import com.extension.company.common.widget.popuwindow.PwTakePhoto;

import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * 商家入驻
 */
public class SellerEnterActivity extends BaseTitleActivity<SellerEnterContract.View, SellerEnterPresenter>
		implements SellerEnterContract.View, View.OnClickListener {

	private static final int REQUEST_PERMISSION = 0;

	private static final int REQUEST_PERMISSION_CAMER = 1;

	@BindView(R.id.tv_topmsg)
	TextView tv_topmsg;

	@BindView(R.id.tv_nature)
	TextView tv_nature;

	@BindView(R.id.tv_trade)
	TextView tv_trade;

	@BindView(R.id.tv_address)
	TextView tv_address;

	@BindView(R.id.mRadioGroup)
	RadioGroup mRadioGroup;

	@BindView(R.id.layout_organizationcode)
	LinearLayout layout_organizationcode;

	@BindView(R.id.tianjiatup)
	ImageView tianJiatUp;

	private PwAddressListAddPopWindow pwAddressListAdd;
	private PwTakePhoto pwTakePhoto;
	private Uri imgUri;

	public static void startActivity(Activity activity) {
		Intent intent = new Intent(activity, SellerEnterActivity.class);
		activity.startActivity(intent);
	}

	@Override
	protected void initData() {
		setTitle("商家入驻");
		tv_topmsg.setText(getResources().getString(R.string.MerchantEnter));
		layout_organizationcode.setVisibility(View.GONE);

		pwAddressListAdd = new PwAddressListAddPopWindow(this);
		pwAddressListAdd.setTitle("企业所在地");

		pwTakePhoto = new PwTakePhoto(this);
	}

	@Override
	protected void initListener() {
		tv_nature.setOnClickListener(this);
		tv_trade.setOnClickListener(this);
		tv_address.setOnClickListener(this);
		tianJiatUp.setOnClickListener(this);
		setOnCheckedChangeListener();
		setOnPwAddressListAddListener();

		pwTakePhoto.setBtnOnclickListener(new PwTakePhoto.OnBtnOnClickListener() {
			@Override
			public void cancel() {

			}

			@Override
			public void openCanmer() {
				imgUri = openCameraUtils.startActivityForCamera(SellerEnterActivity.this);
				pwTakePhoto.dismiss();
			}

			@Override
			public void openGallerys() {
				Intent intentToPickPic = new Intent(Intent.ACTION_PICK, null);
				intentToPickPic.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/jpeg");
				startActivityForResult(intentToPickPic, Constant.SELECT_REQUEST_GALLERY);
				pwTakePhoto.dismiss();

			}
		});
	}

	@Override
	public int getMainLayout() {
		return R.layout.sellerenter;
	}

	@Override
	public SellerEnterContract.View createView() {
		return this;
	}

	@Override
	public SellerEnterPresenter createPresenter() {
		return new SellerEnterPresenter();
	}

	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}

	private void setOnPwAddressListAddListener() {
		pwAddressListAdd.setOnPwAddressListAddListener(new PwAddressListAddPopWindow.OnPwAddressListAddListener() {
			@Override
			public void pwAddressListAdd(String provice, String city, String county) {
				tv_address.setText(provice + city + county);
			}

			@Override
			public void pwAddressListAddId(String proviceId, String cityId, String countyId) {

			}
		});
	}

	private void setOnCheckedChangeListener() {
		mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.rb1) {
					layout_organizationcode.setVisibility(View.GONE);
				} else if (checkedId == R.id.rb2) {
					layout_organizationcode.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.tv_nature:
				//性质
				List<String> natureList = Arrays.asList(getResources().getStringArray(R.array.NatureList));
				//ArrayListUtils.getNatureList()
				WheelPickerUtil.getInstance().showOptionsAlert(this, natureList,
						new WheelPickerUtil.OnOptionsListener() {
							@Override
							public void select(int position, Object data) {
								tv_nature.setText((CharSequence) data);
							}
						});
				break;
			case R.id.tv_trade:
				//性质
				List<String> tradeList = Arrays.asList(getResources().getStringArray(R.array.TradeList));
				//ArrayListUtils.getTradeList()
				WheelPickerUtil.getInstance().showOptionsAlert(this, tradeList,
						new WheelPickerUtil.OnOptionsListener() {
							@Override
							public void select(int position, Object data) {
								tv_trade.setText((CharSequence) data);
							}
						});
				break;
			case R.id.tv_address:
				boolean b = PermissionsUtil.openPermissions(this, PermissionsUtil.WRITE_READ_PERMISSIONS, REQUEST_PERMISSION);
				if (b) {
					pwAddressListAdd.show();
				}
				break;
			case R.id.tianjiatup: // 拍照
				boolean a = PermissionsUtil.openPermissions(this, PermissionsUtil.OPEN_CAMER_PERMISSIONS, REQUEST_PERMISSION_CAMER);
				if (a && pwTakePhoto != null) {
					pwTakePhoto.show();
				}

				break;
			default:
				break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 0) {
			if (resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
				ToastUtil.showToast(this, "你已经拒绝了权限");
			} else {
				pwAddressListAdd.show();
			}
		}

		if (requestCode == REQUEST_PERMISSION_CAMER) {  // 申请摄像头权限
			if (resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
				ToastUtil.showToast(this, "您已经拒绝了权限");
			} else {
				pwTakePhoto.show();
			}
		}

		if (resultCode == RESULT_OK) { // 拍照成功
			switch (requestCode) {
				case Constant.PHOTO_REQUEST_CAMERA:
					Bitmap bitmap = null;
					try {
						Log.d("zwk", "theme" + imgUri.getScheme());
						Log.d("zwk", "author" + imgUri.getAuthority());
						Log.d("zwk", "host" + imgUri.getHost());
						Log.d("zwk", "port" + imgUri.getPort());
						Log.d("zwk", "path" + imgUri.getPath());
						bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imgUri));
						tianJiatUp.setImageBitmap(bitmap);
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					}

					break;
				case Constant.SELECT_REQUEST_GALLERY:
					ContentResolver cr = this.getContentResolver();
					Uri uri = data.getData();
					Log.d("zwk", "相册的内容" + uri);
					Log.d("zwk", uri.getPath());
					String realPath = openCameraUtils.getImagePath(this, uri);
					Log.d("zwk", "真实路径" + realPath);
					try {
						tianJiatUp.setImageBitmap(BitmapFactory.decodeStream(cr.openInputStream(uri)));
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					}
					break;

				default:
					break;

			}

		}
	}
}
